const db = uniCloud.database();
const jdb = uniCloud.databaseForJQL();
const {giveMsg,giveCode,result}=require('shop_common');
//let dbJQL

module.exports = {
	_before: function () { // 通用预处理器
		this.startTime = Date.now()
		
		
		/* dbJQL = uniCloud.databaseForJQL({ // 获取JQL database引用，此处需要传入云对象的clientInfo
			clientInfo: this.getClientInfo()
		})
		dbJQL.setUser({ 
			uid: '664b0e39bd022087df1f3210',
				role: ['admin'], 
		})	 */
	},
	
	async get_index_swiper(){
		let res = await db.collection("shop_index_swiper").where({"state":true}).orderBy("orderId","asc").get()
		//let res = await dbJQL.collection("shop_index_swiper").get()
		
		/* let result = {
			error:0,
			errMsg:"查询成功",
			data:res.data
		}
		return result */
		//使用common改造上述代码
		return result(200,"success",res.data)
	},
	
	async get_index_navigation(){
		let res = await db.collection("shop_index_navigation").where({"state":true}).orderBy("orderId","asc").get()
		return result(200,"success",res.data)
	},
	
	async get_index_floor(){
		let res = await db.collection("shop_index_floor").where({"state":true}).orderBy("orderId","asc").get()
		return result(200,"success",res.data)
	},
	
	async get_categories(){
		let res1 = await db.collection("opendb-mall-categories").where({cat_level:0}).get()
		
		//将所有分类变成树形结构输出，需要4秒多时间，不推荐使用
		/* for(let i=0;i<res1.data.length;i++){
			let res2 = await db.collection("opendb-mall-categories").where({cat_pid:res1.data[i].cat_id}).get()
			for(let i2=0;i2<res2.data.length;i2++){
				if(res2.data[i2].cat_level == 1){
					let res3 = await db.collection("opendb-mall-categories").where({cat_pid:res2.data[i2].cat_id}).get()
					res2.data[i2].children.push(...res3.data)
				}
			}
			res1.data[i].children.push(...res2.data)
		} */
		
		
		let res2 = await db.collection("opendb-mall-categories").where({cat_pid:res1.data[0].cat_id}).get()
		for(let i=0;i<res2.data.length;i++){
			if(res2.data[i].cat_level == 1){
			let res3 = await db.collection("opendb-mall-categories").where({cat_pid:res2.data[i].cat_id}).get()
			res2.data[i].children.push(...res3.data)
			}
		}
		res1.data[0].children.push(...res2.data)
		return result(200,"success",res1.data)
	},
	
	async get_categories_one(){
		let body = this.getHttpInfo().body
		if(!body) throw result(400,"required")
		this.params = JSON.parse(body)
		let {cat_id} = this.params
		if(!cat_id) throw result(400,"required")
		let res2 = await db.collection("opendb-mall-categories").where({cat_pid:cat_id}).get()
		for(let i=0;i<res2.data.length;i++){
			if(res2.data[i].cat_level == 1){
			let res3 = await db.collection("opendb-mall-categories").where({cat_pid:res2.data[i].cat_id}).get()
			res2.data[i].children.push(...res3.data)
			}
		}
		return result(200,"success",res2.data)
	},
	
	async get_goodslist_bycid(){
		let body = this.getHttpInfo().body
		if(!body) throw result(400,"required")
		this.params = JSON.parse(body)
		//return this.params
		
		const res = await uniCloud.httpclient.request('https://api-hmugo-web.itheima.net/api/public/v1/goods/search', {
		    method: 'GET',
		    data: this.params,
		    contentType: 'json', // 指定以application/json发送data内的数据
		    dataType: 'json' // 指定返回值为json格式，自动进行parse
		  })
		return result(200,"success",res.data.message)
	},
	
	async get_goodsDetail_bygoods_id(){
		let body = this.getHttpInfo().body
		if(!body) throw result(400,"required")
		this.params = JSON.parse(body)
		const res = await uniCloud.httpclient.request('https://api-hmugo-web.itheima.net/api/public/v1/goods/detail', {
		    method: 'GET',
		    data: this.params,
		    contentType: 'json', // 指定以application/json发送data内的数据
		    dataType: 'json' // 指定返回值为json格式，自动进行parse
		  })
		return res.data
	},
	
	async get_search_byquery(){
		let body = this.getHttpInfo().body
		if(!body) throw result(400,"required")
		this.params = JSON.parse(body)
		const res = await uniCloud.httpclient.request('https://api-hmugo-web.itheima.net/api/public/v1/goods/qsearch', {
		    method: 'GET',
		    data: this.params,
		    contentType: 'json', // 指定以application/json发送data内的数据
		    dataType: 'json' // 指定返回值为json格式，自动进行parse
		  })
		return res.data
	},
	
	_after(error,result){
		if(error){
			throw error
		}
		result.timeCost = Date.now() - this.startTime
		return result
	}
}
